package org.roger.analyzer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.ParseException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.KeywordAnalyzer;
import org.apache.lucene.analysis.core.LowerCaseFilter;
import org.apache.lucene.analysis.standard.StandardTokenizer;
import org.apache.lucene.analysis.synonym.SynonymFilter;
import org.apache.lucene.analysis.synonym.SynonymMap;
import org.apache.lucene.analysis.synonym.WordnetSynonymParser;

/**
 * 同义词分析器
 * @author luoweiyi
 */
public class SynonymAnalyzer extends Analyzer {

	/**
	 * 先用StandardTokenizer作分词器，再通过LowerCaseFilter和SynonymFilter过滤语汇单元
	 */
	@Override
	protected TokenStreamComponents createComponents(String fieldName) {

		final StandardTokenizer src = new StandardTokenizer();

		TokenStream tok = new LowerCaseFilter(src);
		// tok = new SynonymFilter(tok,engine);
		SynonymMap mySynonymMap = null;
		try {
			mySynonymMap = buildSynonym();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tok = new SynonymFilter(tok, mySynonymMap, false);

		return new TokenStreamComponents(src, tok);

		// return new TokenStreamComponents(new StandardTokenizer(reader),
		// new SynonymFilter(new LetterTokenizer(reader),engine));
	}

	/**
	 * 通过Wordnet同义词库创建SynonymMap
	 * @return
	 * @throws IOException
	 * @throws ParseException
	 */
	private SynonymMap buildSynonym() throws IOException, ParseException {
		File file = new File("D:/study/lucene/prolog/wn_s.pl");
		InputStream stream = new FileInputStream(file);
		Reader rulesReader = new InputStreamReader(stream);
		SynonymMap.Builder parser = new WordnetSynonymParser(true, true, new KeywordAnalyzer());
		((WordnetSynonymParser) parser).parse(rulesReader);
		SynonymMap synonymMap = parser.build();
		return synonymMap;
	}

}
